﻿#region
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using ZhongLangDB;
#endregion

namespace ZHONGLANGWEBUI.ZHONGLANG.SYS
{
    public partial class ROLE : TT_BasePageList
    {
        protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { pageInit(); dgListDo("查询初次", ""); } }

        #region pageInit
        protected override void pageInit()
        {
            DDLBindStr("1$正常,9$停用", true, ref ddlQROLESTATE);
            DDLBindStr("1$正常,9$停用", false, ref ddlEROLESTATE);

            DataTable dtMenu = DBPark.proGetDT(null, "SELECT * FROM TT_SYS_MENU");
            chklEROLEMENU.Items.Clear();
            int iL = chklEROLEMENU.RepeatColumns;
            DataRow[] drMenuS = dtMenu.Select("MENUPRO = 'TT'", "MENUORDER");
            for (int j = 0; j < drMenuS.Length; j++)
            {
                chklEROLEMENU.Items.Add(new ListItem(drMenuS[j]["MENUNAME"].ToString(), drMenuS[j]["MENUID"].ToString()));
                for (int kj = 0; kj < iL - 1; kj++) { chklEROLEMENU.Items.Add(new ListItem("=====", "")); chklEROLEMENU.Items[chklEROLEMENU.Items.Count - 1].Enabled = false; }

                DataRow[] drMenuSJ = dtMenu.Select("MENUPRO = '" + drMenuS[j]["MENUID"].ToString() + "'", "MENUORDER");
                for (int s = 0; s < drMenuSJ.Length; s++)
                {
                    chklEROLEMENU.Items.Add(new ListItem("", ""));
                    chklEROLEMENU.Items.Add(new ListItem(drMenuSJ[s]["MENUNAME"].ToString(), drMenuSJ[s]["MENUID"].ToString()));
                    for (int sj = 0; sj < iL - 2; sj++) { chklEROLEMENU.Items.Add(new ListItem("", "")); chklEROLEMENU.Items[chklEROLEMENU.Items.Count - 1].Enabled = false; }

                    DataRow[] drMenuTJ = dtMenu.Select("MENUPRO = '" + drMenuSJ[s]["MENUID"].ToString() + "'", "MENUORDER");
                    Double iCount = Convert.ToDouble(drMenuTJ.Length) / (iL - 2);
                    int iRow = Convert.ToInt16(Math.Ceiling(iCount));
                    int iTT = 0;
                    for (int jj = 0; jj < iL * iRow; jj++)
                    {
                        if (jj % iL == 0 || jj % iL == 1) { chklEROLEMENU.Items.Add(new ListItem("", "")); chklEROLEMENU.Items[chklEROLEMENU.Items.Count - 1].Enabled = false; continue; }
                        if (drMenuTJ.Length > iTT) { chklEROLEMENU.Items.Add(new ListItem(drMenuTJ[iTT]["MENUNAME"].ToString(), drMenuTJ[iTT]["MENUID"].ToString())); iTT++; }
                        else { chklEROLEMENU.Items.Add(new ListItem("", "")); chklEROLEMENU.Items[chklEROLEMENU.Items.Count - 1].Enabled = false; }
                    }
                }
            }
        }
        #endregion

        #region pageBind 
        protected DataTable proSDListQuery(int iType)
        {
            SortedList SDList = new SortedList();
            SDList["ROLENAME"] = txtQROLENAME.Text;
            SDList["ROLESTATE"] = ddlQROLESTATE.SelectedValue;
            DataTable dt = pgShow.proQueryData(SDList, "PROTT_SYS_ROLEQUERY", iType);
            return dt;
        }

        protected override void pageBind()
        {
            DataTable dt = proSDListQuery(1);
            DataView dv = dt.DefaultView;
            if (ViewState["SortExpression"] != null) { dv.Sort = GetSortDirectiont(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString()); }
            dgList.DataSource = dv; dgList.DataBind(); 
        }
        #endregion

        #region pageOpt 
        protected override void dgListDo(string strCommandName, string strCommandArgument)
        {
            #region dgBase 
            if (strCommandName == "查询初次") { pgShow.CurrPageIndex = 1; pgShow.CurrPageSize = REQPAGECOUNT; pageBind(); }
            if (strCommandName == "查询") { pageBind(); tabQuery.Visible = true; tabShow.Visible = true; tabEdit.Visible = false; }
            if (strCommandName == "导出") { proExpData(proSDListQuery(2), dgList); }
            if (strCommandName == "关闭") { tabQuery.Visible = true; tabShow.Visible = true; tabEdit.Visible = false; }
            if (strCommandName == "添加") { lblUpdate.Text = ""; btnSave.CommandArgument = ""; tabQuery.Visible = false; tabShow.Visible = false; tabEdit.Visible = true; }
            #endregion


            #region 修改 
            if (strCommandName == "修改")
            {
                SortedList SDList = new SortedList();
                SDList["KEYID"] = strCommandArgument;
                DataTable dt = DBPark.proGetDT(SDList,  "PROTT_SYS_ROLEQUERY");
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0]; 
                    txtEROLECODE.Text = dr["ROLECODE"].ToString();
                    txtEROLENAME.Text = dr["ROLENAME"].ToString();
                    txtEROLEINFO.Text = dr["ROLEINFO"].ToString();
                    DDLBindValue(dr["ROLESTATE"].ToString(), ref ddlEROLESTATE);
                    DDLBindValue(dr["ROLEMENU"].ToString(), ref chklEROLEMENU);

                    lblUpdate.ForeColor = System.Drawing.Color.Blue;
                    lblUpdate.Text = dr["UPDATEMANNAME"].ToString() + "【" + strDateFmtTime(dr["UPDATETIME"].ToString()) + "编辑】";
                    btnSave.CommandArgument = strCommandArgument; tabQuery.Visible = false; tabEdit.Visible = true; tabShow.Visible = false;

                }
                else { jsMessage("参数有误，未找到对应记录！"); }
            }
            #endregion

            #region 保存
            if (strCommandName == "保存")
            {
                SortedList SDList = new SortedList();
                SDList["ROLECODE"] = txtEROLECODE.Text;
                SDList["ROLENAME"] = txtEROLENAME.Text;
                SDList["ROLEINFO"] = txtEROLEINFO.Text;
                SDList["ROLESTATE"] = ddlEROLESTATE.SelectedValue;
                SDList["ROLEMENU"] = DDLBindValueCheckValueGet(chklEROLEMENU);
                SDList["KEYID"] = btnSave.CommandArgument; SDList["UPDATEMANID"] = CURREMP.USERID; SDList["UPDATEMANNAME"] = CURREMP.USERNAME;
                string strBack = DBPark.proSetDB(SDList,  "PROTT_SYS_ROLESAVE");
                if (strBack != "") { lblUpdate.Text = strBack; lblUpdate.ForeColor = System.Drawing.Color.Red; jsMessage(strBack); }
                else { dgListBaseDo("关闭", ""); }
            }
            #endregion 
        }
        #endregion
          
    }
}